<%@page import="chances.system.operator.entity.MgmtInfo"%>
<%@page import="chances.system.operator.service.MgmtInfoService"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=utf-8"	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"	scope="request" />
<spring:eval expression="@config['site.picture']" var="sitePicPath" scope="application"/>

<%
	ServletContext context = request.getSession().getServletContext();
	ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
	MgmtInfoService mgmtInfoService = (MgmtInfoService)ctx.getBean("mgmtInfoService");
	MgmtInfo mgmtInfo = mgmtInfoService.findFirstOrDeFault();
	application.setAttribute("mgmtInfo", mgmtInfo);
%>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>登录</title>
    	<link rel="shortcut icon" href="${contextPath}${sitePicPath}/${mgmtInfo.favicon}" type="image/x-icon">
		<link href="css/bootstrap.min.css" rel="stylesheet">
		<link href="css/animate.css" rel="stylesheet">
		<link href="css/style.css" rel="stylesheet">
    	<link href="css/plugins/toastr/toastr.min.css" rel="stylesheet">
		<script src="js/jquery-2.1.1.js"></script>
		<script src="js/plugins/toastr/toastr.min.js"></script>
		
		<script>
			toastr.options = {
			    closeButton: true,
			    progressBar: true,
			    showMethod: 'slideDown',
			    timeOut: 4000
			};
			if(window.parent != window){
				window.parent.location.href = window.location.href; 
			}
	
			function changeRand(){
				document.getElementById("randImg").src="${contextPath}/sys/validateCode/generate.action?rand='/>"+Math.random();
			}
	
			function valdateCode(){
				var code = document.getElementById("code").value;
				if(code==""){
					toastr.warning("请输入验证码");
					return false;
				}
				 
				$.ajax({
		 	 	    type:"GET",
		 	 	    async:false,
		 	 	    url:"${contextPath}/sys/validateCode/"+$("#code").val()+".action",
		 	 	    success:function(data){
			 	 	    if(data.result == 0){
			 				document.getElementById("loginForm").submit();
			 		    }else{
			 		    	toastr.error(data.message);
			 		    	changeRand();
			 				return false;
			 			}
		 	 	    }
				});
				return false;
			}
		</script>
	</head>
	
	<body class="gray-bg">
		<div class="row" style="height:25%"></div>
		<div style="height:70%;" class="loginColumns animated fadeInDown">
			<div class="row">
				<div class="col-md-6">
					<img src="${contextPath}${sitePicPath}/${mgmtInfo.logo}" style="width:300px;height:100px">
					<h3 class="font-bold" style="margin:20px 0">${mgmtInfo.title}</h3>
				</div>
				<div class="col-md-6">
					<div class="login-body">
							<p class="text-danger">
								<c:choose>
									<c:when test="${not empty param.errorMessage && param.errorMessage=='error.concurrentSession'}">
										相同帐号在另一地点登陆
									</c:when>
									<c:otherwise>
										<c:if test="${not empty SPRING_SECURITY_LAST_EXCEPTION}">
											<c:set var="errorMsg" value="${SPRING_SECURITY_LAST_EXCEPTION.message}"></c:set>
											<c:choose>
												<c:when test="${fn:contains(errorMsg,'用户已被禁用')}">
													${SPRING_SECURITY_LAST_EXCEPTION.message}
												</c:when>
												<c:otherwise>
													用户名或密码错误
												</c:otherwise>
											</c:choose>	
										</c:if>
									</c:otherwise>
								</c:choose>
													
							</p>
						<c:remove var="SPRING_SECURITY_LAST_EXCEPTION"/>
						<form id="loginForm" class="form-horizontal" role="form" method="post" action="spring_security_check" onsubmit="return valdateCode();">
							<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>							
							<div class="form-group">
								<input type="text" name="username" class="col-ma-12 form-control" placeholder="用户名" required>
							</div>
							<div class="form-group">
								<input type="password" name="password" class="form-control" placeholder="密码" required>
							</div>
							<div class="form-group input-group col-sm-4" style="white-space:nowrap">
								<input type="text" name="code" id="code" class="form-control" size="2"/> 
								<img src="${contextPath}/sys/validateCode/generate.action" onclick="changeRand()" id="randImg" />
							</div>
							<button type="submit" class="btn btn-primary block full-width m-b">登录</button>
						</form>
					</div>
				</div>
			</div>
		</div>
		<div style="height:5%" class="row">
			<div class="col-md-3"></div>
			<div class="col-md-6">
				<div class="col-md-6">@${mgmtInfo.copyright}版权所有</div>
				<div class="col-md-6 text-right">©2017</div>
			</div>
		</div>
	</body>
</html>
